package hzjbbis.framework.workflow.service.impl;

import hzjbbis.framework.workflow.entity.Comment;
import hzjbbis.framework.workflow.entity.Form;
import hzjbbis.framework.workflow.service.FormService;
import hzjbbis.framework.workflow.service.AbstractService;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.util.TextUtils;

public class FormServiceImpl extends AbstractService implements FormService {
    private static final Log log = LogFactory.getLog(FormServiceImpl.class);

    public void createForm(Form form) {
        pm.create(form);
    }

    public void updateForm(Form form) {
        pm.update(form);
    }

    public void deleteForm(Form form) {
        pm.delete(form);
    }

    public Form loadFormById(long id) {
        return (Form) qm.loadById(Form.class, id);
    }

    public Form findFormByFormId(long formNo) {
        List l = qm.findByNamedQuery("findFormByFormId",new Long(formNo));

        if (l != null && l.size() != 0) {
            return (Form) l.get(0);
        }
        return null;
    }

    public void addComment(Form form, Comment comment) {
        form.addComment(comment);
        pm.create(comment);
    }

    public Form[] findFormByWorkflowIds(List wfIdList) {
        if (wfIdList == null || wfIdList.size() < 1)
            return null;
        String query = "from Form form where form.workflowId in ("
            + TextUtils.join(", ", wfIdList) + ")";
        List result = qm.find(query);
        return (Form[]) result.toArray(new Form[result.size()]);
    }
}
